﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using br.com.SistemaChamados.Enum;
using SistemaChamados.App_Code.DAO.QueryManager.Behavior;
using System.Data;

namespace br.com.SistemaChamados.DAO.QueryManager
{
    public static class QueryManager
    {
        public static List<object> Select(Type _typeClass, ConnectionBehavior behavior)
        {
            List<object> result = new List<Object>();
            IDbConnection _conn = Mapped.Connection(); //TODO: verificar se não é melhor uma ConnectionFactory
            string _sql = buildASelectSql(_typeClass);
            IDbCommand _command = Mapped.Command(_sql, _conn);
            IDataReader _reader = _command.ExecuteReader(behavior.getConnectionBehavior());
            result = processAndConvertDataReader(_typeClass, _reader);
            return result;
        }

        public static object Select(Type _typeClass, object identifier, ConnectionBehavior behavior)
        {
            //TODO: implementation
            return new object();
        }

        public static EnumCRUDState Update(object updatableObject, object identifier, ConnectionBehavior behavior)
        {
            //TODO: implementation
            return EnumCRUDState.SQL_SUCCESS;
        }

        public static EnumCRUDState Delete(object deletableObject, object identifier, ConnectionBehavior behavior)
        {
            //TODO: implementation
            return EnumCRUDState.SQL_SUCCESS;
        }

        private static List<object> processAndConvertDataReader(Type _typeClass, IDataReader _reader)
        {
            List<object> listConvertedDataReader = new List<object>();
            return listConvertedDataReader;
        }

        private static string buildASelectSql(Type _typeClass)
        {
            string tableName = QueryManagerConstants.PREFIX_TABLE+ _typeClass.Name;
            string aSql = QueryManagerConstants.TEMPLATE_SQL_SELECT;
            aSql = aSql.Replace(QueryManagerConstants.EXPRESSION_TABLE_NAME, tableName);
            return aSql;
        }
    }
}